{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>文本列1</th>\n",
       "      <th>文本列2</th>\n",
       "      <th>文本列3</th>\n",
       "      <th>数值</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>甲</td>\n",
       "      <td>一部门</td>\n",
       "      <td>A</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>甲</td>\n",
       "      <td>一部门</td>\n",
       "      <td>B</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>C</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>D</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>E</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>F</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>G</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>H</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>乙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>I</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>乙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>J</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>K</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>L</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>M</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>N</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>O</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>P</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>Q</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>R</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>S</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   文本列1 文本列2 文本列3   数值\n",
       "0     甲  一部门    A   80\n",
       "1     甲  一部门    B   30\n",
       "2     甲  二部门    C   40\n",
       "3     甲  二部门    D  100\n",
       "4     甲  二部门    E   80\n",
       "5     乙  一部门    F   20\n",
       "6     乙  一部门    G   60\n",
       "7     乙  一部门    H   70\n",
       "8     乙  二部门    I   30\n",
       "9     乙  二部门    J   90\n",
       "10    乙  一部门    K  100\n",
       "11    丙  一部门    L   50\n",
       "12    丙  一部门    M   90\n",
       "13    丙  一部门    N   80\n",
       "14    丙  一部门    O   10\n",
       "15    丙  二部门    P   40\n",
       "16    丙  二部门    Q   50\n",
       "17    丙  二部门    R   30\n",
       "18    丙  二部门    S   70"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel('../toy_dataset.xlsx',sheet_name='raw_data')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>文本列1</th>\n",
       "      <th>文本列2</th>\n",
       "      <th>文本列3</th>\n",
       "      <th>数值</th>\n",
       "      <th>国际通用排名</th>\n",
       "      <th>中国式排名</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>甲</td>\n",
       "      <td>一部门</td>\n",
       "      <td>A</td>\n",
       "      <td>80</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>甲</td>\n",
       "      <td>一部门</td>\n",
       "      <td>B</td>\n",
       "      <td>30</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>C</td>\n",
       "      <td>40</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>D</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>甲</td>\n",
       "      <td>二部门</td>\n",
       "      <td>E</td>\n",
       "      <td>80</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>F</td>\n",
       "      <td>20</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>G</td>\n",
       "      <td>60</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>H</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>乙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>I</td>\n",
       "      <td>30</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>乙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>J</td>\n",
       "      <td>90</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>乙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>K</td>\n",
       "      <td>100</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>L</td>\n",
       "      <td>50</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>M</td>\n",
       "      <td>90</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>N</td>\n",
       "      <td>80</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>丙</td>\n",
       "      <td>一部门</td>\n",
       "      <td>O</td>\n",
       "      <td>10</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>P</td>\n",
       "      <td>40</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>Q</td>\n",
       "      <td>50</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>R</td>\n",
       "      <td>30</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>丙</td>\n",
       "      <td>二部门</td>\n",
       "      <td>S</td>\n",
       "      <td>70</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   文本列1 文本列2 文本列3   数值  国际通用排名  中国式排名\n",
       "0     甲  一部门    A   80       2      2\n",
       "1     甲  一部门    B   30       5      4\n",
       "2     甲  二部门    C   40       4      3\n",
       "3     甲  二部门    D  100       1      1\n",
       "4     甲  二部门    E   80       2      2\n",
       "5     乙  一部门    F   20       6      6\n",
       "6     乙  一部门    G   60       4      4\n",
       "7     乙  一部门    H   70       3      3\n",
       "8     乙  二部门    I   30       5      5\n",
       "9     乙  二部门    J   90       2      2\n",
       "10    乙  一部门    K  100       1      1\n",
       "11    丙  一部门    L   50       4      4\n",
       "12    丙  一部门    M   90       1      1\n",
       "13    丙  一部门    N   80       2      2\n",
       "14    丙  一部门    O   10       8      7\n",
       "15    丙  二部门    P   40       6      5\n",
       "16    丙  二部门    Q   50       4      4\n",
       "17    丙  二部门    R   30       7      6\n",
       "18    丙  二部门    S   70       3      3"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['国际通用排名'] = df['数值'].groupby(df['文本列1']).rank(method='min',ascending=False).astype(int)\n",
    "df['中国式排名'] = df['数值'].groupby(df['文本列1']).rank(method='dense',ascending=False).astype(int)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.to_excel('./result_python.xlsx',index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
